package cn.cqautotest.cqviehelper.utils;

import android.content.Context;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;
import com.vondear.rxtool.RxTool;

import cn.cqautotest.cqviehelper.R;
import cn.cqautotest.cqviehelper.beans.AppData;
import cn.cqautotest.cqviehelper.beans.UpdateApp;
import cn.cqautotest.cqviehelper.config.AppConfig;
import cn.cqautotest.cqviehelper.config.Constants;
import cn.cqautotest.cqviehelper.utils.network.MyOkHttp;
import cn.cqautotest.cqviehelper.utils.network.model.HttpData;

/**
 * 更新APP的工具类
 */
public class UpdateAppDataUtils {

    private static final String TAG = UpdateAppDataUtils.class.getSimpleName();

    /**
     * 检查APP更新
     */
    public static void updateApp(Context context) {
        RxTool.getBackgroundHandler().post(() -> {
            try {
                AppData appData = getAppData();
                String updateAppUrl = appData.getUpdateApp();
                String updateAppInfo = MyOkHttp.postData(updateAppUrl, null, null);
                HttpData<UpdateApp> updateAppHttpData = GsonUtils.fromJson(updateAppInfo, new TypeToken<HttpData<UpdateApp>>() {
                }.getType());
                UpdateApp updateApp = updateAppHttpData.getData();
                ThreadUtils.getMainHandler().post(() -> checkAppVersion(context, updateApp));
            } catch (Exception e) {
                e.printStackTrace();
                ThreadUtils.getMainHandler().post(() -> ToastUtils.showShort("APP数据更新失败！"));
            }
        });
    }

    /**
     * 获取APP数据
     * @return AppData
     * @throws Exception
     */
    public static AppData getAppData() throws Exception {
        String serverUrl = Constants.APP_SERVER_URL;
        String appDataInfo = MyOkHttp.postData(serverUrl, null, null);
        HttpData<AppData> appDataHttpData = GsonUtils.fromJson(appDataInfo, new TypeToken<HttpData<AppData>>() {
        }.getType());
        return appDataHttpData.getData();
    }

    /**
     * 检查APP版本并判断是否更新
     *
     * @param context
     * @param updateApp
     */
    private static void checkAppVersion(Context context, UpdateApp updateApp) {
        // 本地的版本码和服务器的进行比较
        int currentVersionCode = AppConfig.getVersionCode();
        // 服务器的最新版本
        float latestVersion = updateApp.getVersionCode();
        //LogUtils.d(TAG, "checkAppVersion: fileMD5 is ========> " +updateApp.getFileMd5());
        if (currentVersionCode < latestVersion) {
            //String downloadUrl = "http://app.mi.com/download/505080?id=com.suda.yzune.wakeupschedule&ref=appstore.mobile_download&nonce=4652825007372684833%3A26614106&appClientId=2882303761517485445&appSignature=kwvh5n1DQxmo96QNtMRwVwdyZpW5Pxy2TeBA92poum8";
//            new UpdateDialog.Builder(context)
//                    // 版本名
//                    .setVersionName(String.valueOf(updateApp.getVersionCode()))
//                    // 是否强制更新
//                    .setForceUpdate(updateApp.isForceUpdate())
//                    // 更新日志
//                    .setUpdateLog(updateApp.getUpdateLog())
//                    // 下载 url
//                    //.setDownloadUrl("https://raw.githubusercontent.com/getActivity/AndroidProject/master/AndroidProject.apk")
//                    .setDownloadUrl(updateApp.getDownloadUrl())
//                    //设置下载的文件的md5值
//                    .setFileMD5(updateApp.getFileMd5())
//                    .show();
        } else if (currentVersionCode > updateApp.getVersionCode()) {
            ToastUtils.showShort("当前版本软件已被篡改！");
        } else {
            ToastUtils.showShort(R.string.update_no_update);
        }
    }
}
